<template>
  <div>
    <!--顶部空白-->
    <div style="height: 3rem;"></div>
    <van-circle v-model="currentRate" :rate="100" :color='gradientColor' :speed="200" :text="status"
                style="margin-top: 5%"/>

    <van-cell title="上次检测时间" :label="detectionTime" style="margin-top: 5%">
      <template #right-icon>
        <van-button @click="routerHelper('antigenResultDetail')">查看详情</van-button>
      </template>
    </van-cell>
  </div>
</template>

<script>
import { Dialog } from 'vant';
export default {
  name: 'AntigenResult',
  data() {
    return {
      currentRate: 0,
      status: '',
      gradientColor: {
        '0%': '#e1e1e1',
        '100%': '#8c8c8c'
      },

      identity: '',
      latestDetectionData: {},
      detectionTime: ''
    }
  },
  mounted() {
    this.getUserIdentity();
    this.getLatestDetectionData()
  },
  methods: {

    routerHelper(arg) {
      this.$router.push('/' + arg)
    },

    getUserIdentity(){
      var userInfo = JSON.parse(localStorage.getItem("userInfo"));
      this.identity = userInfo.identity;
    },

    // 通过身份证号获取个人最新一次抗原检测结果
    getLatestDetectionData() {
      this.$axios(
          {
            method: "post",
            url: "http://localhost:8085/detection/getLatestDetectionData",
            data: {
              identity: this.identity
            }
          }
      ).then(res => {
        if (res.data.code === 200) {
          this.latestDetectionData = res.data.data;
          this.detectionTime = this.latestDetectionData.detectionTime;
          this.setCircle(this.latestDetectionData.detectResult);
        }
        else{
          Dialog({
            message: res.data.message,
            theme: 'round-button',
          }).then(() => {
            this.routerHelper('antigenHome');
          });
        }
      })
    },

    // 根据抗原检测结果设置对应的颜色和结果
    setCircle(detectResult) {
      if (detectResult === 1){
        this.status = '阳性';
        this.gradientColor["0%"] = '#f87373';
        this.gradientColor["100%"] = '#f61010';
      }
      if (detectResult === 2){
        this.status = '阴性';
        this.gradientColor["0%"] = '#81FBB8';
        this.gradientColor["100%"] = '#28C76F';
      }
      if (detectResult === 3){
        this.status = '无效结果';
        this.gradientColor["0%"] = '#e1e1e1';
        this.gradientColor["100%"] = '#8c8c8c';
      }
    }
  }
}
</script>


